/* 数字金额逢三加， 比如 123,464.23 */
export function numberToCurrency(values) {
  if (values == undefined) return 
  let value = Number(values)
  //  数据格式是存在小数点的
  if (String(value).indexOf(".") != -1) {
    if (!value) return '0.00'
    // 将数值截取，保留两位小数
    value = Number(value).toFixed(2)
    // 获取整数部分
    const intPart = Math.trunc(value)
    // 整数部分处理，增加,
    const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    // 预定义小数部分
    let floatPart = '.00'
    // 将数值截取为小数部分和整数部分
    const valueArray = value.toString().split('.')
    if (valueArray.length === 2) { // 有小数部分
      floatPart = valueArray[1].toString() // 取得小数部分
      return intPartFormat + '.' + floatPart
    }
    return intPartFormat + floatPart
  }else {
    let intPart = Math.trunc(value)
    if (intPart >= 10000) {
      intPart = (intPart / 1000).toFixed(2)
      const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
      return intPartFormat
    }else {
      const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
      return intPartFormat
    }
  }
}

export function numberToCurrency2(values) {
  if (values == undefined) return;
  let value = Number(values)
  if (!value) {return '0'}
    //let intPart = Math.trunc(value)
    let intPart = parseInt(value)
    let intPartNew = (intPart / 10000);
    return Number(intPartNew).toFixed(4) ;
}

export function toThousands(nums) {
	let num = Number(nums)
  let intPartFormat = ''
  if (num >= 10000) {
    num = Math.round(num / 1000)
    intPartFormat = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')

  } else {
    intPartFormat = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  }
  //  获取小数点后面的数字。后面用来拼接
  // let floatNum = num.substring(num.indexOf('.') + 1, num.length)

  let result = '';

  while (intPartFormat.length > 3) {
    result = intPartFormat.slice(-3) + result;
    intPartFormat = intPartFormat.slice(0, intPartFormat.length - 3);
  }
  if (intPartFormat) {
    result = intPartFormat + result;
  }
  return result;
}

export function numberMath(value) {
    let intPart = Math.trunc(value)
    if (intPart >= 10000) {
      intPart = (intPart / 10000).toFixed(2)
      const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')

      return intPartFormat
    } else {
      const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
      return intPartFormat
    }
}

export function tranNumber(num, point){
   // 将数字转换为字符串,然后通过split方法用.分隔,取到第0个
   let numStr = num.toString().split('.')[0]
   if(numStr.length<5) { // 判断数字有多长,如果小于6,,表示10万以内的数字,让其直接显示
       return numStr;
     }else if(numStr.length>=5){ // 如果数字大于6位,小于8位,让其数字后面加单位万
        let decimal = numStr.substring(numStr.length-4, numStr.length-4+point)
        // 由千位,百位组成的一个数字
        return parseFloat(parseInt(num / 1000)+'.'+decimal); 
   }
}
